<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/ftl/js/vue/vue.min.js"></script>

    <script src="/js/jquery.validation/1.14.0/jquery.validate.min.js"></script>
    <script src="/js/jquery.validation/1.14.0/messages_zh.min.js"></script>
    <style>
        input.error{
            border: 1px solid #E6594E;
        }
    </style>
</head>
<body>
<div class="container" id="app">
    <form id="addUserForm">
        <br/>
        <div class="form-group">
            <label for="username">用户名称：</label>
            <input type="text" class="form-control" id="username" name="username" placeholder="输入名称">
        </div>
        <div class="form-group">
            <label for="userage">用户年龄：</label>
            <input type="text" class="form-control" id="userage" name="userage" placeholder="输入年龄">
        </div>
        <div class="form-group">
            <label for="userage">用户部门：</label>
            <select id="dept">
                <option v-for="(item,index) in dept" :value="item.deptid">{{item.deptname}}</option>
            </select>
        </div>
        <div class="form-group">
            <button type="button" id="saveBtn" class="btn btn-success">提交</button>
            <button type="button" id="cancelBtn" class="btn btn-default">取消</button>
        </div>
    </form>
</div>
</body>
<script>
    let vue = new Vue({
        el:'#app',
        data:{
            dept:[]
        },
        method:{

        }
    })
    $.ajax({
        type:"post",
        url:"/getDepts",
        success:function (res){
            vue.dept = res
        }
    })
    $("#saveBtn").click(function (){
        if (!check().form()){
            return;
        }
        console.log("Name:", $("#username").val());
        console.log("Age:", $("#userage").val());
        console.log("Dept ID:", $("#dept").val());
        $.ajax({
            type:"post",
            url:"/addUser",
            contentType: "application/json",
            data:JSON.stringify({
                name: $("#username").val(),
                age: $("#userage").val(),
                dept:{
                    deptid: $("#dept").val()
                }
            }),
            success:function (res){
                $("#cancelBtn").click()
            }
        })
    })

    $("#cancelBtn").click(function (){
        var index = parent.layer.getFrameIndex(window.name);
        parent.getUserList();
        parent.layer.close(index);
    })

    function check(){
        return $("#addUserForm").validate({
            rules:{
                username:{
                    required: true
                },
                userage:{
                    required: true
                }
            },
            message:{
                username:{
                    required:""
                },
                userage:{
                    required: ""
                }
            }
        })
    }
</script>
</html>
